package rubiks.cube;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;


/**
 * INTERFACE GRAFICA DO PROJETO DE LEDA 2011.2 
 * 
 * @authors Diego de Lima, Dimitre Andrew, Isabelle Cardoso e Rennan Henrique
 *
 */
public class RubiksCube extends JFrame{
	
		private Cube cube;
		
	 	private final int WHITE = 1;
	 	private final int GREEN = 2;
	 	private final int RED = 3;
	 	private final int BLUE = 4;
	 	private final int ORANGE = 5;
	 	private final int YELLOW = 6;
		private static final long serialVersionUID = -5002173823861123982L;
		
		private int X;
		private int Y;
		
		public static void main(String[] args) {
			new RubiksCube();
		}
			    
		public RubiksCube() {
			
			super("Projeto de LEDA 2011.2");			
			
			cube = new Cube();			
			X = 600;
			Y = 500;
			setDefaultCloseOperation(EXIT_ON_CLOSE);
			setSize(X,Y);
			setLocation(0,0); 
			Container container = getContentPane();
			Buttons buttons = new Buttons();
			drawRC draw = new drawRC();			
			JSplitPane splitPaneRight = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, draw, buttons);
			splitPaneRight.setDividerLocation(X - 150);
		    setLocationRelativeTo(null);  
			container.add(splitPaneRight);			
			validate();
			setVisible(true);
			
			boolean exit = false;
			while (!exit) {
				repaint();
			}	
		}

		public Cube getCube() {
			return cube;
		}
		
		/**
		 * Frame do Guia de Ajuda
		 */
		public class MyJFrame extends JFrame {
			
			private static final long serialVersionUID = -5002173823861123982L;

			   /**
			   * Construtor que instancia um JPanel e o adiciona a este JFrame.
			   */			
			  public MyJFrame() {
			
			    this.setTitle("Guia de ajuda");
			    
			    this.setSize(365, 250);
			   
			    addWindowListener(new WindowAdapter() {}); 
			    
			    Image image = null;
			    try {
				    File file = new File("src/img/helpguide.png");
				    image = ImageIO.read(file);
			    }
			    catch(Exception exp1){

			    } 
			    
			    JLabel label = new JLabel(new ImageIcon(image));
			    getContentPane().add(label, BorderLayout.CENTER);
			    pack(); 
			    setLocationRelativeTo(null); 
			    
			    setVisible(true);
			    
			  }
		}
		
		/**
		 * Desenho do Cubo Maico na Interface Grafica
		 */
		private class drawRC extends JPanel{
		
			private static final long serialVersionUID = 7169866537591383965L;

			public drawRC() {}
			
			public void drawSqr(Graphics2D g, int x, int y, Color color) {
				g.setColor(color);
				g.fillRect(x*3, y*3, 30, 30);
			}
			
			public void paintComponent(Graphics _g) {
				
				Graphics2D g = (Graphics2D) _g;				
				g.drawString("CUBO MAGICO 3x3", 120, 35);				
				g.drawString("Resolvido: "+cube.isSolved(),120, 420);
			
				int cubeX = 5;
				int cubeY = 20;
				
				//DESENHA QUADRADOS NA GUI
				drawSqr(g, cubeX + 36,cubeY, getColor(cube.getPiece(0, 0, 2).getBack()));
				drawSqr(g, cubeX + 48,cubeY, getColor(cube.getPiece(1, 0, 2).getBack()));
				drawSqr(g, cubeX + 60,cubeY, getColor(cube.getPiece(2, 0, 2).getBack()));
				drawSqr(g, cubeX + 36,cubeY + 12, getColor(cube.getPiece(0, 1, 2).getBack()));
				drawSqr(g, cubeX + 48,cubeY + 12, getColor(cube.getPiece(1, 1, 2).getBack()));
				drawSqr(g, cubeX + 60,cubeY + 12, getColor(cube.getPiece(2, 1, 2).getBack()));
				drawSqr(g, cubeX + 36,cubeY + 24, getColor(cube.getPiece(0, 2, 2).getBack()));
				drawSqr(g, cubeX + 48,cubeY + 24, getColor(cube.getPiece(1, 2, 2).getBack()));
				drawSqr(g, cubeX + 60,cubeY + 24, getColor(cube.getPiece(2, 2, 2).getBack()));

				drawSqr(g, cubeX ,cubeY + 36, getColor(cube.getPiece(0, 0, 2).getLeft()));
				drawSqr(g, cubeX + 12,cubeY + 36, getColor(cube.getPiece(0, 1, 2).getLeft()));
				drawSqr(g, cubeX + 24,cubeY + 36, getColor(cube.getPiece(0, 2, 2).getLeft()));
				drawSqr(g, cubeX ,cubeY + 48, getColor(cube.getPiece(0, 0, 1).getLeft()));
				drawSqr(g, cubeX + 12,cubeY + 48, getColor(cube.getPiece(0, 1, 1).getLeft()));
				drawSqr(g, cubeX + 24,cubeY + 48, getColor(cube.getPiece(0, 2, 1).getLeft()));
				drawSqr(g, cubeX ,cubeY + 60, getColor(cube.getPiece(0, 0, 0).getLeft()));
				drawSqr(g, cubeX + 12,cubeY + 60, getColor(cube.getPiece(0, 1, 0).getLeft()));
				drawSqr(g, cubeX + 24,cubeY + 60, getColor(cube.getPiece(0, 2, 0).getLeft()));
				
				drawSqr(g, cubeX + 36,cubeY + 36, getColor(cube.getPiece(0, 2, 2).getTop()));
				drawSqr(g, cubeX + 48,cubeY + 36, getColor(cube.getPiece(1, 2, 2).getTop()));
				drawSqr(g, cubeX + 60,cubeY + 36, getColor(cube.getPiece(2, 2, 2).getTop()));
				drawSqr(g, cubeX + 36,cubeY + 48, getColor(cube.getPiece(0, 2, 1).getTop()));
				drawSqr(g, cubeX + 48,cubeY + 48, getColor(cube.getPiece(1, 2, 1).getTop()));
				drawSqr(g, cubeX + 60,cubeY + 48, getColor(cube.getPiece(2, 2, 1).getTop()));
				drawSqr(g, cubeX + 36,cubeY + 60, getColor(cube.getPiece(0, 2, 0).getTop()));
				drawSqr(g, cubeX + 48,cubeY + 60, getColor(cube.getPiece(1, 2, 0).getTop()));
				drawSqr(g, cubeX + 60,cubeY + 60, getColor(cube.getPiece(2, 2, 0).getTop()));

				drawSqr(g, cubeX + 72,cubeY + 36, getColor(cube.getPiece(2, 2, 2).getRight()));
				drawSqr(g, cubeX + 84,cubeY + 36, getColor(cube.getPiece(2, 1, 2).getRight()));
				drawSqr(g, cubeX + 96,cubeY + 36, getColor(cube.getPiece(2, 0, 2).getRight()));
				drawSqr(g, cubeX + 72,cubeY + 48, getColor(cube.getPiece(2, 2, 1).getRight()));
				drawSqr(g, cubeX + 84,cubeY + 48, getColor(cube.getPiece(2, 1, 1).getRight()));
				drawSqr(g, cubeX + 96,cubeY + 48, getColor(cube.getPiece(2, 0, 1).getRight()));
				drawSqr(g, cubeX + 72,cubeY + 60, getColor(cube.getPiece(2, 2, 0).getRight()));
				drawSqr(g, cubeX + 84,cubeY + 60, getColor(cube.getPiece(2, 1, 0).getRight()));
				drawSqr(g, cubeX + 96,cubeY + 60, getColor(cube.getPiece(2, 0, 0).getRight()));
				

				drawSqr(g, cubeX + 108,cubeY + 36, getColor(cube.getPiece(2, 0, 2).getBottom()));
				drawSqr(g, cubeX + 120,cubeY + 36, getColor(cube.getPiece(1, 0, 2).getBottom()));
				drawSqr(g, cubeX + 132,cubeY + 36, getColor(cube.getPiece(0, 0, 2).getBottom()));
				drawSqr(g, cubeX + 108,cubeY + 48, getColor(cube.getPiece(2, 0, 1).getBottom()));
				drawSqr(g, cubeX + 120,cubeY + 48, getColor(cube.getPiece(1, 0, 1).getBottom()));
				drawSqr(g, cubeX + 132,cubeY + 48, getColor(cube.getPiece(0, 0, 1).getBottom()));
				drawSqr(g, cubeX + 108,cubeY + 60, getColor(cube.getPiece(2, 0, 0).getBottom()));
				drawSqr(g, cubeX + 120,cubeY + 60, getColor(cube.getPiece(1, 0, 0).getBottom()));
				drawSqr(g, cubeX + 132,cubeY + 60, getColor(cube.getPiece(0, 0, 0).getBottom()));
				
				drawSqr(g, cubeX + 36,cubeY + 72, getColor(cube.getPiece(0, 2, 0).getFront()));
				drawSqr(g, cubeX + 48,cubeY + 72, getColor(cube.getPiece(1, 2, 0).getFront()));
				drawSqr(g, cubeX + 60,cubeY + 72, getColor(cube.getPiece(2, 2, 0).getFront()));
				drawSqr(g, cubeX + 36,cubeY + 84, getColor(cube.getPiece(0, 1, 0).getFront()));
				drawSqr(g, cubeX + 48,cubeY + 84, getColor(cube.getPiece(1, 1, 0).getFront()));
				drawSqr(g, cubeX + 60,cubeY + 84, getColor(cube.getPiece(2, 1, 0).getFront()));
				drawSqr(g, cubeX + 36,cubeY + 96, getColor(cube.getPiece(0, 0, 0).getFront()));
				drawSqr(g, cubeX + 48,cubeY + 96, getColor(cube.getPiece(1, 0, 0).getFront()));
				drawSqr(g, cubeX + 60,cubeY + 96, getColor(cube.getPiece(2, 0, 0).getFront()));
	
			} 
			
			
			
			public Color getColor(int color) {
				if(color == WHITE) {
					return Color.white;
				} else if(color == YELLOW) {
					return Color.yellow;
				} else if(color == RED) {
					return Color.red;
				} else if(color == ORANGE) {
					return Color.orange;
				} else if(color == BLUE) {
					return Color.blue;
				} else if(color == GREEN) {
					return Color.green;
				}
				return Color.black;
			}
		}
		
		/**
		 * Botoes do painel lateral
		 */
		private class Buttons extends JPanel implements ActionListener {
	
			private static final long serialVersionUID = -9059135647883556109L;

			private JButton reset;
			private JButton scramble;
			private JButton solve;
			
			private JButton b, bi, f,fi,l,li,r,ri,u,ui,d,di;
			private JButton guide;
			private JTextArea nomes;
			
			
			public Buttons() {
				
				nomes = new JTextArea("" +
						"Projeto de LEDA \n" +
						"--------------\n" +
						"Diego de Lima \n" +
						"Dimitre Andrew \n" +
						"Isabelle Cardoso \n" +
						"Rennan Henrique \n");
				nomes.setBounds(20, 350, 100, 100);		
				nomes.setBackground(Color.BLACK);
				nomes.setForeground(Color.WHITE);
				add(nomes);
				
				guide = new JButton("Help");
				guide.setBounds(20, 150, 100, 20);
				add(guide);
				guide.setBackground(Color.RED);
				guide.addActionListener(this);
				
				this.setLayout(null);
				reset = new JButton("Reset");
				reset.setBackground(Color.YELLOW);
				reset.setBounds(20, 10, 100, 15);
				add(reset);
				reset.addActionListener(this);
				
				scramble = new JButton("Scramble");
				scramble.setBackground(Color.YELLOW);
				scramble.setBounds(20, 25, 100, 15);
				add(scramble);				
				scramble.addActionListener(this);
				
				solve = new JButton("Solve");
				solve.setBackground(Color.YELLOW);
				solve.setBounds(20, 40, 100, 15);
				add(solve);
				solve.addActionListener(this);
				
				b = new JButton("B");
				b.setBounds(20, 55, 50, 15);
				add(b);				
				b.addActionListener(this);
				
				bi = new JButton("Bi");
				bi.setBounds(70, 55, 50, 15);
				add(bi);				
				bi.addActionListener(this);
				
				f = new JButton("F");
				f.setBounds(20, 70, 50, 15);
				add(f);				
				f.addActionListener(this);
				
				fi = new JButton("Fi");
				fi.setBounds(70, 70, 50, 15);
				add(fi);
				fi.addActionListener(this);	
				
				u = new JButton("U");
				u.setBounds(20, 85, 50, 15);
				add(u);
				u.addActionListener(this);
				
				ui = new JButton("Ui");
				ui.setBounds(70, 85, 50, 15);
				add(ui);
				ui.addActionListener(this);
				
				d = new JButton("D");
				d.setBounds(20, 100, 50, 15);
				add(d);
				d.addActionListener(this);
				
				di = new JButton("Di");
				di.setBounds(70, 100, 50, 15);
				add(di);
				di.addActionListener(this);
				
				
				l = new JButton("L");
				l.setBounds(20, 115, 50, 15);
				add(l);
				l.addActionListener(this);
				
				li = new JButton("Li");
				li.setBounds(70, 115, 50, 15);
				add(li);
				li.addActionListener(this);
				
				r = new JButton("R");
				r.setBounds(20, 130, 50, 15);
				add(r);
				r.addActionListener(this);
				
				ri = new JButton("Ri");
				ri.setBounds(70, 130, 50, 15);
				add(ri);
				ri.addActionListener(this);
				
			}
			
			//Chama o frame do guia
			public void guide(){
			
				new MyJFrame();
			}
			
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == reset) {
					cube.reset();
				} else if(e.getSource() == scramble) {
					cube.scramble(100);
				}  else if(e.getSource() == solve) {
					cube.solve();
				}  else if(e.getSource() == guide) {
					guide();
				} else if(e.getSource() == b) {
					cube.B();
				} else if(e.getSource() == bi) {
					cube.Bi();
				} else if(e.getSource() == f) {
					cube.F();
				} else if(e.getSource() == fi) {
					cube.Fi();
				} else if(e.getSource() == l) {
					cube.L();
				} else if(e.getSource() == li) {
					cube.Li();
				} else if(e.getSource() == r) {
					cube.R();
				} else if(e.getSource() == ri) {
					cube.Ri();
				} else if(e.getSource() == u) {
					cube.U();
				} else if(e.getSource() == ui) {
					cube.Ui();
				} else if(e.getSource() == d) {
					cube.D();
				} else if(e.getSource() == di) {
					cube.Di();
				}
			}
		}
	
}